﻿Imports SIGI.Common.BE
Imports System.Data.SqlClient
Imports System.Configuration

Public Class ScreenDAC

#Region "<< -Search- >>"
    Public Shared Function Search() As List(Of ScreenBE)
        Dim wSqlConn As SqlConnection = Nothing
        Dim wSqlCmd As SqlCommand = Nothing
        Dim wReader As SqlDataReader = Nothing
        Dim wConnStr As String = String.Empty

        Dim wScreenBEList As List(Of ScreenBE) = Nothing
        Dim wScreenBE As ScreenBE = Nothing

        Try
            'Obtengo la cadena de conexión
            wConnStr = ConfigurationManager.ConnectionStrings("connString").ConnectionString
            'Creo la conexión a la BD
            wSqlConn = New SqlConnection(wConnStr)
            'Creo el comando
            wSqlCmd = New SqlCommand()
            wSqlCmd.Connection = wSqlConn
            wSqlCmd.CommandType = CommandType.StoredProcedure
            wSqlCmd.CommandText = "Pantallas_s"

            'Abro la conexión a la BD
            wSqlConn.Open()

            wReader = wSqlCmd.ExecuteReader()
            'Leo los resultados de la consulta
            wScreenBEList = New List(Of ScreenBE)
            While wReader.Read()
                wScreenBE = New ScreenBE
                wScreenBE.Id = Convert.ToInt32(wReader("ID_Pantalla"))
                wScreenBE.Description = Convert.ToString(wReader("Descripcion_Pant"))

                wScreenBEList.Add(wScreenBE)
            End While
            'Cierro la conexión a la BD
            wSqlConn.Close()
        Catch ex As Exception
            Throw ex
        Finally
            wReader = Nothing
            wSqlCmd = Nothing
            wSqlConn = Nothing
        End Try

        Return wScreenBEList
    End Function
#End Region

#Region "<< -SearchScreenPermissionByProfile- >>"
    Public Shared Function SearchScreenPermissionByProfile(pIdProfile As Integer, pIdScreen As Integer) As Integer
        Dim wSqlConn As SqlConnection = Nothing
        Dim wSqlCmd As SqlCommand = Nothing
        Dim wReader As SqlDataReader = Nothing
        Dim wConnStr As String = String.Empty
        Dim wIdPermission As Integer = 0
        Try
            'Obtengo la cadena de conexión
            wConnStr = ConfigurationManager.ConnectionStrings("connString").ConnectionString
            'Creo la conexión a la BD
            wSqlConn = New SqlConnection(wConnStr)
            'Creo el comando
            wSqlCmd = New SqlCommand()
            wSqlCmd.Connection = wSqlConn
            wSqlCmd.CommandType = CommandType.StoredProcedure
            wSqlCmd.CommandText = "PerfilesxPantallas_g_Permiso"

            wSqlCmd.Parameters.Add("@Id_Perfil", SqlDbType.Int)
            wSqlCmd.Parameters.Add("@Id_Pantalla", SqlDbType.Int)

            wSqlCmd.Parameters("@Id_Perfil").Value = pIdProfile
            wSqlCmd.Parameters("@Id_Pantalla").Value = pIdScreen

            wSqlConn.Open()

            wReader = wSqlCmd.ExecuteReader()
            If wReader.Read() Then
                If Not wReader("ID_Permiso") Is DBNull.Value Then
                    wIdPermission = Convert.ToInt32(wReader("ID_Permiso"))
                End If
            End If

            wSqlConn.Close()
        Catch ex As Exception
            Throw ex
        Finally
            wReader = Nothing
            wSqlCmd = Nothing
            wSqlConn = Nothing
        End Try

        Return wIdPermission
    End Function
#End Region

    '#Region "<< -SearchByProfileId- >>"
    '    Public Shared Function SearchByProfileId(pProfileId As Integer) As List(Of ScreenBE)
    '        Dim wSqlConn As SqlConnection = Nothing
    '        Dim wSqlCmd As SqlCommand = Nothing
    '        Dim wReader As SqlDataReader = Nothing
    '        Dim wConnStr As String = String.Empty

    '        Dim wScreenBEList As List(Of ScreenBE) = Nothing
    '        Dim wScreenBE As ScreenBE = Nothing

    '        Try
    '            'Obtengo la cadena de conexión
    '            wConnStr = ConfigurationManager.ConnectionStrings("connString").ConnectionString
    '            'Creo la conexión a la BD
    '            wSqlConn = New SqlConnection(wConnStr)
    '            'Creo el comando
    '            wSqlCmd = New SqlCommand()
    '            wSqlCmd.Connection = wSqlConn
    '            wSqlCmd.CommandType = CommandType.StoredProcedure
    '            wSqlCmd.CommandText = "Pantallas_s_PorPerfilId"

    '            'Abro la conexión a la BD
    '            wSqlConn.Open()

    '            wReader = wSqlCmd.ExecuteReader()
    '            'Leo los resultados de la consulta
    '            wScreenBEList = New List(Of ScreenBE)
    '            While wReader.Read()
    '                wScreenBE = New ScreenBE
    '                wScreenBE.Id = Convert.ToInt32(wReader("ID_Pantalla"))
    '                wScreenBE.Description = Convert.ToString(wReader("Descripcion_Pant"))

    '                wScreenBEList.Add(wScreenBE)
    '            End While
    '            'Cierro la conexión a la BD
    '            wSqlConn.Close()
    '        Catch ex As Exception
    '            Throw ex
    '        Finally
    '            wReader = Nothing
    '            wSqlCmd = Nothing
    '            wSqlConn = Nothing
    '        End Try

    '        Return wScreenBEList
    '    End Function
    '#End Region

End Class
